public class Solution {
    public boolean isValid(String s) {
        LinkedList<String> stack = new LinkedList<>();
        String[] ss = s.split("");
        for(String as : ss) {
            if(index(as) < 3) {
                stack.push(as);
            } else {
                if(index(stack.peek()) + index(as) == 5) {
                    stack.pop();
                } else {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }

    public int index(String s) {
        String[] t = {"(", "{", "[", "]", "}", ")"};
        for(int i = 0; i < 6; i++) {
            if(t[i].equals(s)) {
                return i;
            }
        }
        return -1;
    }
}